package com.atguigu.wulaoshi

object MyWhile {
  def main(args: Array[String]): Unit = {
    var n = 10
    //1.常规while循环
    while(n>=1){
      print(n)
      n=n-1
    }
    println("==========================")
    //2.用闭包实现一个函数,将代码块作为参数传入,递归调用
    def myWhile(condition: =>Boolean):( =>Unit)=>Unit = {
      //内层函数需要递归调用
      def doLoop(op: =>Unit): Unit = {
        if(condition){
          op
          myWhile(condition)(op)
        }
      }
      doLoop
    }
    n=10
    myWhile(n>=1)({
      print(n)
      n=n-1
    })
    println("===========================")
    //3.用匿名函数实现
    def myWhile1(condition: =>Boolean):( =>Unit)=>Unit = {
      op=>{
        if(condition){
          op
          myWhile1(condition)(op)
        }
      }
    }
    n=10
    myWhile1(n>=1)({
      print(n)
      n=n-1
    })
    println("=============================")
    //4.用柯里化实现
    def myWhile2(condition: =>Boolean)(op: =>Unit):Unit= {
      if(condition){
        op
        myWhile2(condition)(op)
      }
    }
    n=10
    myWhile2(n>=1){
      print(n)
      n=n-1
    }

  }
}
